package com.example.controller;

import com.example.dao.LabDAO;
import com.example.model.Lab;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/labs")
public class LabServlet extends HttpServlet {
    private LabDAO labDAO = new LabDAO();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");
        if (action == null) {
            action = "";
        }

        switch (action) {
            case "add":
                addLab(request, response);
                break;
            case "edit":
                editLab(request, response);
                break;
            case "update":
                updateLab(request, response);
                break;
            case "delete":
                deleteLab(request, response);
                break;

            default:
                listLabs(request, response);
                break;
        }
    }

    private void listLabs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Lab> labs = labDAO.findAllLabs();
        request.setAttribute("labs", labs);
        request.getRequestDispatcher("/jsp/lab/list.jsp").forward(request, response);
    }

    private void addLab(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String labName = request.getParameter("labName");
        String location = request.getParameter("location");
        int capacity = Integer.parseInt(request.getParameter("capacity"));
        String equipment = request.getParameter("equipment");
        String availableTime = request.getParameter("availableTime");
        String description = request.getParameter("description");

        Lab lab = new Lab();
        lab.setLabName(labName);
        lab.setLocation(location);
        lab.setCapacity(capacity);
        lab.setEquipment(equipment);
        lab.setAvailableTime(availableTime);
        lab.setDescription(description);

        boolean success = labDAO.addLab(lab);
        if (success) {
            response.sendRedirect("labs");
        } else {
            request.setAttribute("error", "添加失败");
            request.getRequestDispatcher("/jsp/lab/add.jsp").forward(request, response);
        }
    }

    private void editLab(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long id = Long.parseLong(request.getParameter("id"));
        Lab lab = labDAO.findLabById(id);
        if (lab != null) {
            request.setAttribute("lab", lab);
            request.getRequestDispatcher("/jsp/lab/edit.jsp").forward(request, response);
        } else {
            request.setAttribute("error", "实验室不存在");
            request.getRequestDispatcher("/jsp/lab/list.jsp").forward(request, response);
        }
    }

    private void updateLab(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long id = Long.parseLong(request.getParameter("id"));
        String labName = request.getParameter("labName");
        String location = request.getParameter("location");
        int capacity = Integer.parseInt(request.getParameter("capacity"));
        String equipment = request.getParameter("equipment");
        String availableTime = request.getParameter("availableTime");
        String description = request.getParameter("description");

        Lab lab = new Lab();
        lab.setLabId(id);
        lab.setLabName(labName);
        lab.setLocation(location);
        lab.setCapacity(capacity);
        lab.setEquipment(equipment);
        lab.setAvailableTime(availableTime);
        lab.setDescription(description);

        boolean success = labDAO.updateLab(lab);
        if (success) {
            response.sendRedirect("labs");
        } else {
            request.setAttribute("error", "更新失败");
            request.getRequestDispatcher("/jsp/lab/edit.jsp").forward(request, response);
        }
    }

    private void deleteLab(HttpServletRequest request, HttpServletResponse response) throws IOException {
        long id = Long.parseLong(request.getParameter("id"));
        boolean success = labDAO.deleteLab(id);
        if (success) {
            response.sendRedirect("labs");
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "删除失败");
        }
    }
}